• Resolved chilimanjaro

    (@chilimanjaro)


    Hi,

    I was wondering how I can change 2 things:
    1. I want to make hyperlinks out of titles of menu items to redirect to this specific item’s website. How can I achieve that? I don’t know what type of code I should add in title.php.
    2. I want limit a description on a main menu list to about 100 characters, but leave everywhere else as it is right now.

    Thanks in advance!

    https://www.ads-software.com/plugins/food-and-drink-menu/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi chilimanjaro,

    1. The $this object which renders the menu item’s title.php template includes the post’s ID. You can use that to access any information not already part of the object. So it should work (not tested myself) if you change title.php to:

    <p class="fdm-item-title">
    	<a href="<?php echo get_permalink( $this->id ); ?>">
    		<?php echo $this->title; ?>
    	</a>
    </p>

    2. This is a little bit more complicated depending on how sophisticated you want to get with it. I believe you can customize the output by using the is_single() template tag. So to chop off the content after 100 characters you would modify content.php to look something like this:

    <div class="fdm-item-content">
    	<?php echo is_single() ? $this->content : substr( $this->content, 0, 100 ); ?>
    </div>

    However, that’s just a rough chop — it will split the middle of words, HTML tags or anything. If you want a prettier excerpt, you’ll need to write something to handle the menu item content string.

    Thread Starter chilimanjaro

    (@chilimanjaro)

    Hi,

    Thank you for your reply. First one worked like a charm. As for the second one, it worked after adding $this->content into is_single(). However it is not the result that I expected. Let me elaborate it.

    When there is a description set for an item, I want it to be shortened ONLY in menu list (possibly finishing with “…” when a sentence is cut off), but when visiting a specific item’s website or embedding it in a homepage, I would like it to have a full description. The reason is that we might be adding for example a gallery there for more pictures of a dish.

    I understand that this might be a pain, but is there a possibility to do this?

    Thanks you very much for help.

    I’m not sure what you mean by “it worked after adding $this->content into is_single()”. Please post the code you’re using.

    is_single() will check if a single post or page is being viewed. So it will return true (and print the full content) when the single post type template is being rendered (single-fdm-menu-item.php > single.php, see Template Hierarchy). If you also want to show a full version on the homepage, you can try adding the is_home() template tag as well.

    <?php echo is_single() || is_home() ? $this->content : substr( $this->content, 0, 100 ); ?>

    I would recommend

    Thread Starter chilimanjaro

    (@chilimanjaro)

    What I meant was I used:

    <div class="fdm-item-content">
    	<?php echo is_single($this->content) ? $this->content : substr( $this->content, 0, 100 ); ?>
    </div>

    The one you provided didn’t work for me. But now I know I shouldn’t have done that. I don’t know much about PHP and didn’t quite understand that, but I was messing around and that code cut off the sentence. I’ve just read about ternary operators and now I understand more of what is happening here.

    Now I am wondering why doesn’t it display a value of substr( $this->content, 0, 100 ) when I am looking at a menu list. Might my version of WordPress (3.6.1) interfere with that? I am sort of new to that engine.

    Hi chilimanjaro,

    On second thought, is_single() alone won’t work in this case because the menu post type and a page will also trigger as true. Try this instead:

    <?php echo ( is_single() && get_post_type() == 'fdm-menu-item' ) || is_home() ? $this->content : substr( $this->content, 0, 100 ); ?>

    It’s saying: show the full content if we’re viewing a single page of post type ‘fdm-menu-item’ OR we’re on the homepage.

    Thread Starter chilimanjaro

    (@chilimanjaro)

    Hi NateWr,

    It’s working perfectly right now! Thank you very much for your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Editing Menu Layout’ is closed to new replies.