Wordpress - ABC filter - wordpress

I would like to list out the alphabet on a custom post type archive page, and by clicking on a letter in the alphabet it would link to a url that only displays posts that begin with that particular letter. Not necessarily worried about doing it ajax style or anything. Just a simple url will work.
I have this functionality on a site I developed a couple years ago (http://glenwoodia.com/business-directory/), but that was using a big business directory plugin. With this particular site I'm just using a custom post type with custom fields.
My full loop currently looks like this:
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('orderby=title'.'&order=ASC'.'&showposts=15'.'&post_type=businesses'.'&paged='.$paged);
?>
<div class="content">
<div class="main-area-wide">
<div class="internal-wide">
<div class="navigation-top">
<?php wp_pagenavi(); ?>
</div>
<div class="business-wrapper">
<h1>Business Directory</h1>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="business">
<h3><?php the_title(); ?></h3>
<p><?php $key="address"; echo get_post_meta($post->ID, $key, true); ?><br />
<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?><br />
<strong><?php $key="phone"; echo get_post_meta($post->ID, $key, true); ?></strong></p>
<p><?php $key="short_description"; echo get_post_meta($post->ID, $key, true); ?></p>
</div>
<?php endwhile; ?>
</div>
<div class="navigation-btm">
<?php wp_pagenavi(); ?>
</div>
</div>
</div>
</div>

Sorry, this should be added as a comment, but I don't have the rep to do that. Anyway, I believe this is what you need and best of all it's documented well. I have used it before and found it pretty easy to understand:
http://www.kathyisawesome.com/424/alphabetical-posts-glossary/
Basically the same answer is also posted in Stack Exchange, be sure to scroll down to the EDIT part of the answer as that part explains the using a taxonomy thing that she did in the answer above:
https://wordpress.stackexchange.com/questions/41660/how-to-build-a-directory-with-indexes-in-wordpress/

Related

Showing prev/next post links doesn't work

I have blog which is in sub directory and showing last post on static index page on main site.
main site -> http://example.com
blog -> http://example.com/wp
The blog post is showed correctly on main site but I can't show links for prev and next post/article. This is what I have an what I'm trying
<?php
define('WP_USE_THEMES', false);
require('wp/wp-blog-header.php');
$posts = get_posts('numberposts=1&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?>
</h1><h2><?php the_date(); echo "<br />";?></h2>
<h3><?php the_content(); ?> </h3>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>
<?php endforeach; ?>
So this isn't visible: <div class="navigation"><p><?php posts_nav_link(); ?></p></div>
In Layman language it is a wordpress function to fetch posts/custom-posts. Internally it calls WP_Query function. Or you can use WP_Query instead of this. 'posts_nav_link' function does not work with WP_Query & get_posts function. You can try this to get next previous links.
$posts = get_posts('numberposts=1&order=ASC&orderby=post_title');
foreach ($posts as $post) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?>
</h1><h2><?php the_date(); echo "<br />";?></h2>
<h3><?php the_content(); ?> </h3>
<?php endforeach; ?>
<div class="navigation">
<p><?php previous_post_link(); ?><?php next_post_link(); ?></p>
</div>
I cant comment you because I haven't enough reputations,
First follow https://codex.wordpress.org/Template_Tags/get_posts link
then check the sample code.
and also check the post display setting in the wp-admin panel /wp-admin/options-reading.php settings->reading section.

Wordpress - How do I show only the 4 newest posts on my page?

I am trying to only load the 4 latest posts on my page and I have found a line of code that should work. The problem is that I don't know where to put it.
<?php query_posts('posts_per_page=20'); ?>
This is what I found when I was searching for a solution (although I don't know if this will also work for showing only the 4 newest posts. As you can see I already have 'catname=projecten' on that line and I have no idea how to combine the two.
<?php
query_posts('catname=projecten');
while (have_posts()) : the_post();?>
<div class="col-md-3">
<div class="newsfeed center">
<h1><?php echo get_the_title( $ID ); ?> </h1>
<p>
<?php the_content(); ?>
</p>
</div>
</div>
<?php endwhile;
?>
I hope someone can help me out!
I'd like to answer it, because there are few things need to be corrected. This should do it, and notes below.
<?php
query_posts('category_name=projecten&posts_per_page=4');
while (have_posts()) : the_post(); ?>
<div class="col-md-3">
<div class="newsfeed center">
<h2><?php echo get_the_title(); ?> </h2>
<?php the_content(); ?>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
A few notes:
It's category_name, not catname.
You should not use <h1> for each title, for a better accessibility use <h2> or <h3> etc.
get_the_title( $ID ); the $ID seems not necessary to be there.
Don't wrap the_content(); with <p> it's rich content already, use <div> if you need.
Don't forget to reset the query afterwards.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
http://codex.wordpress.org/Function_Reference/query_posts
http://codex.wordpress.org/Class_Reference/WP_Query

custom wordpress theme echoes gallery short code

So i'm quite a beginner, trying to make a custom theme. In a page i want to have a gallery. Uploaded images, made a gallery all fine.
When I view the page it outputs only the shortcode:
[gallery orderby="post_date"]
my page.php file basically has:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo $content->post_content ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I really don't understand how to get this to show correctly, any pointers would be greatly appreciated. Cheers, Matt
get_page returns the raw page data. There are a few ways to do what you want:
BAD WAY:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo do_shortcode($content->post_content); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
do_shortcode() renders all registered shortcode that's found within a given string. In this case, your page's content will have all shortcode rendered before being written to the document. I say this is the "bad" way, only because it doesn't follow the usual Wordpress format. Which leads us to the:
BETTER WAY:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<h1><?php the_title(); ?></h1><br>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is what's called "The Loop". It is pretty much the standard for all Wordpress Themes in retrieving all Post or Page Data, as well as running queries against the database.
I would suggest getting to know it, as well as running Wordpress queries to modify the loop using WP Query. This is getting into a more complex area of Wordpress, but it will help you in the longrun with figuring out how to gather up all of the posts and pages you want to retrieve in your theme that aren't provided by Wordpress' globals.
Good luck.

Wordpress - Custom Post - Custom Fields

I've installed a plugin for Wordpress Titled "CMS Press" onto my Site.
I have created two custom fields within a custom post I've called "The Team"
The code for the page is as follows :
<div class="span16"> <!-- This is column 1 -->
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="pagehead"><h2 class="title_page"><?php echo get_post_meta($post->ID, 'title_page', true);?></h2></div>
<div class="span6 subcontent">
<?php
$args = array ('post_type' => 'team');
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="teamsubhead">
<h2 class="team_page"><?php the_title(); ?></h2>
<h3 class="job_title"><?php get_post_meta($post->ID, 'jobtitle', true); ?></h3>
</div>
<?php the_content(); ?>
<?php endforeach; ?>
</div>
<?php endwhile; ?>
</div>
The custom field I created in my custom post is called "jobtitle" I am trying to retrieve that from this line of code
<h3 class="job_title"><?php get_post_meta($post->ID, 'jobtitle', true); ?></h3>
However nothing is getting output at all.
Can someone help me with this. I suspect it may be something to do with the loop and my new query being in a new loop?
Thanks in advance.
Try it with echo at the beginning:
<?php echo get_post_meta($post->ID, 'jobtitle', true); ?>

WordPress featured post and sharethis issue

im having some trouble with this featured post function that I made. the issue that im having is that when query('showposts=1'); is set the featured post is not picked up. however when I put query(''); the sharethis plugin doesn't work. can any one please help me on what I might be doing wrong.
<div id="block_feature">
<div id="featured_post" class="post">
<div class="post_inner">
<?php
$featured = new WP_Query();
$featured->query('showposts=1');
while($featured->have_posts()) : $featured->the_post();
//$wp_query->in_the_loop = true; // This line is added so that the_tags('') will work outside the regular loop.
$featured_ID = $post->ID; // We'll store this here so that we know to skip this post in the main loop
?>
<?php if(get_post_meta($post -> ID, 'feature', true)) { ?>
<?php if (get_post_meta($post->ID, 'large_preview', true)) { ?>
<div class="post_image">
<img src="<?php echo get_post_meta($post->ID,'large_preview',true);?>" width=150px; height=150px alt="Featured Post"/>
</div>
<?php } ?>
<div class="excerpt">
<h2><?php the_title(); ?></h2>
<small>on <?php the_time('M d'); ?> in <?php the_category(',');?> tagged <?php the_tags(''); ?></small>
<?php the_excerpt();?>
</div>
Read More
<?php } ?>
<?php endwhile; ?>
</div>
</div>
</div>
showposts is been deprecated since version 2.1, so it might be better to use posts_per_page instead. Not sure if it will make any difference, but you could also replace the first two lines of PHP with $featured = new WP_Query('showposts=1');
As to what is causing the problem I am not sure, your query looks fine to me and you didn't mention in what way Share This failed. I'm not familiar with the Share This plugin, but most such plugins add its content to the post using a filter function attached to the the_content filter. That said, it might just be that you are using the_excerpt() and not the_content().

Resources