Displaying first few words of WordPress page in the nav - php

I'm wanting to display a WordPress nav that has the first few words of each page below it.
This is what I currently have:
<?php wp_list_pages('title_li=&link_after=<span>FIRST FEW WORDS HERE</span>'); ?>
I want the output to be like:
<li>Home<span>Welcome to the website</span></li>
Any help will be appreciated

The only thing I could think of is doing this:
use wp_get_pages to get all the titles (or the ones you want), and then access the database with a query that will get each post using the title. So, for each title from the array the get_pages returned, you get a corresponding post. In either the SQL or the PHP use the substring method to get the first few words, and display however you want.
That's just the logic, I'm a bit rusty with code so I don't want to confuse you. I'll try if you don't get it...

I don't think there's a way to put extra text inside the anchor by just passing params to wp_list_pages. You could however filter it and use preg_replace. For example...
add_filter('wp_list_pages', function($data) {
return preg_replace('/(<a[^>]+[^<]+)<\/a>/si', '\\1<span>FIRST FEW WORDS HERE</span></a>', $data);
});
Though I used preg_replace() you could also (and probably should) use DomDocument but this will work for most use cases.
Another way to do it would be to use a custom walker

Another option is to make a custom loop and add it in the "index.php" file in your theme where you want to display it.
A quick example of how that would look:
<div id="nav"><ul>
<!-- start the loop-->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="nav_post">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<span><?php formatPreview(the_content()); ?></span></a>
</li>
<!-- stop the loop-->
<?php endwhile; else: ?>
<p>No posts!</p>
<?php endif; ?>
</ul></div>
the_permalink(), the_title(), and the_content() are the key players here to populate your custom nav with the post information. Then just add appropriate css for the nav ID, nav_post class, and h2/p and you're good to go! You'll probably want to hard stop the loop after a certain number of posts - I doubt you want every post listed in the nav!
You'll have to make the "formatPreview()" function separately to control how to format the preview of each post's content - but it shouldn't be hard to just substr the first few words!

Related

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

How do you include the Post ID within the_content() field in PHP?

Please bear with me on this question. I understand that it may not be as clear but and suggestions are helpful.
So I created a template page and within that template page I have two tags.
One field is the default:
<?php the_content(); ?>
and the other one is
<?php the_field('test_advertisement_two', 25017); ?>
that I created using ACF (Advanced Custom Fields) which includes the Post id. How can I include the post id in the <?php the_content(); ?> field?
I think I maybe confused how <?php the_content(); ?> works. Is there a way to assign a name to it a name to it?
This is what I am doing:
I am calling the template page (test-template.php) that contains:
<?php the_content(); ?>
<?php the_field('test_advertisement_two', 25017); ?>
from a different location by using
The post content in <?php the_field('test_advertisement_two', 25017); ?> displays but the content from <?php the_content(); ?> does not display. That's why I figure that I either need to include a post Id in <?php the_content(); ?>
The post content in displays but the content from does not display. That's why I figure that I either need to include a post Id in
Essentially.
the_content only functions right within The Loop, one of the most important concepts in WordPress. The the_field call is working because it's being explicitly passed a post ID in the optional second parameter.
You'll need to access it like this:
$page = get_post(25017);
echo $page->post_content;
(or get_page if you're on a page instead of a post)
Hi if you need post content using post id then below is the code for that.
echo get_post_field('post_content', $post_id);

Store and Echo PHP with Wordpress

I'm using the Advanced Custom Fields plugin for WP which I really like, because it gives me the ability to add a whole bunch of custom meta boxes to individual pages within my site, and implementation within page templates is very straightforward. This is generally how I would use it with a template:
<?php if( get_field('FIELD-NAME-HERE') ): ?>
<h6><?php the_field('FIELD-NAME-HERE'); ?></h6>
<?php endif; ?>
In this case, I have a field for my Contact Form 7 Shortcode, and I've turned formatting off for this field, so it should return exactly what is entered (ie. [contact-form-7 title="Contact Form"] ).
I know that I can use the do_shortcode(); to pull this shortcode into my page template, but I want to be able to populate that do_shortcode(); with the_field(); from above.
I've tried this:
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode("<?php the_field('form_shortcode'); ?>");?>
<?php endif; ?>
And this:
<?php
var formCode = the_field('form_shortcode');
echo do_shortcode(formCode);
?>
I'm still pretty new to PHP, but I feel like I'm not too far off. Any help would be greatly appreciated!
You're not far off. You need to use get_field() rather than the_field(): the_field() actually echoes out the content, so that's why it's not working as currently using it. It's the equivalent of trying to echo it twice.
Another thing to look out for is that you need to include the square brackets when using do_shortcode() but since your code already includes them you don't need to worry about it in this instance.
<?php if( get_field('form_shortcode') ): ?>
<?php echo do_shortcode( get_field('form_shortcode') );?>
<?php endif; ?>

How to implement conditional CSS in this situation?

Am using this function
http://bavotasan.com/2012/a-better-wp_link_pages-for-wordpress/
And calling it within template with this
<center><span class="post-pagination"><?php custom_wp_link_pages(); ?></span></center>
However, the CSS shows even on pages that don't contain multipaged articles.
So I guess I need something like
<?php if $multipage then <center><span class="post-pagination"><?php custom_wp_link_pages(); ?></span></center><?php endif; ?>
But obviously this would result in errors.
Need help with syntax for including in the template or proper way of implementing it into the function itself.
$multipage is a variable that already exists and contains the data that tells if the current page is multi or no.
$multipage is a global variable, so you can use following;
<?php
if ( $multipage ) :
?>
<center><span class="post-pagination"><?php custom_wp_link_pages(); ?></span></center
<?php
endif;
?>
From my point of view, first you create the template for the page in which you need pagination.
Then select that newly created template in page. That's it.

WordPress: How to display only posts that are in a certain category?

I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..
However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.
Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
I have a post that if I go to it directly works correctly, but does not show up on this page.
The post direct link.
The category id is '3' while the category name is 'typographic-posters'.
I have a custom page template for the typographic-posters page that looks like this:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Using this code however the page only shows gets the header, sidebar and nothing else..
If someone could help me out that would really help me get a handle on this filtering of wordpress categories.
Thanks for reading,
Jannis
in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.
Once obtained, just use the normal WordPress loop to access these posts.
The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.
in_category will only work outside of the loop on a single page. I
suggest using the query_posts function to solve this problem. You may
use query_posts('cat=3') or
query_posts('category_name=typographic-posters') to get the posts you
are looking for.
Once obtained, just use the normal WordPress loop to access these
posts.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Simply add before the loop:
<?php query_posts="cat=3&showposts=5">
This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).
I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.
But if you need to stick with a Page to display those posts, also see the Page of Posts example.
http://codex.wordpress.org/Template_Tags/query_posts
Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.
This plugin could also help you if you want to be able to change the displayed categories without going through the code :
http://wordpress.org/extend/plugins/advanced-category-excluder/
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need

Categories