Using Symbols in Wordpress Permalinks / URLs - asp.net

The short problem: Editing the sanitize function has allowed me to add uppercase letters and symbols to my permalinks. However, when I use a symbol (like: ".", "=", "?") the link ends up in a 404 error. I'm guessing because the database can't access the page with a URL like that.
Are there any ways around this?
The long problem: I'm transitioning someone's site that was written in ASP on a windows server into a wordpress site. I've copied each page's content and created a wordpress site that duplicates the site structure of the current ASP one. But I need to keep the URL's the same, and his current site pages look like "http://thesite.com/ContentPage.aspx?page=subpage".

You can create a page hierarchy, apply a rewrite rule and create a page template, to solve the problem without having to add extra content to the permalink and the final URL would end up looking like the old ASP URL.
The query variable 'page' holds the pagenumber for a single paginated Post or Page, using this queryvar isn't recommended. You should review the need to maintain the url structure.
Taking this page structure for example:
Animals (permalink: http://thesite.com/animals/)
Lion (permalink: http://thesite.com/animals/lion)
Zebra (permalink: http://thesite.com/animals/zebra)
Others (permalink: http://thesite.com/animals/others)
Flowers (permalink: http://thesite.com/flowers/)
If I access http://thesite.com/Animals.aspx?page=Lion I should be presented the "Lion" page
For this to work you have to add to your functions file the add_rewrite_rule function with the following regex:
^([\w\d\-]*)(?:\.aspx?).*$
(I'm not used to regexp so i guess there's an easier way to write it.)
resulting in something like this:
add_rewrite_rule('^([\w\d\-]*)(?:\.aspx?).*$','index.php?pagename=$matches[1]','top');
Use it in a function like the example in the Codex. This way you'll access http://thesite.com/index.php?pagename=Animals when entering http://thesite.com/Animals.aspx?page=Lion in the address bar.
Do not forget to flush and regenerate the rewrite rules database after modifying rules.
So now you must get the 'page' queryvar and do a custom query to get the page children. The best way to do this is creating a page template. Create a file aspx-template.php in your theme folder and add the following:
<?php
/**
* Template Name: aspx Template
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
$pagename = get_post_field( 'post_name', get_the_ID() );
if ( isset($_GET['page']) ) : $pagename .= '/' . sanitize_text_field( $_GET['page'] ); endif;
$args = array(
'post_type' => 'page',
'pagename' => $pagename,
'post_status' => 'publish'
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<article>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<div>
No page found
</div>
<?php endif; ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
Don't forget to associate the template to the parent page (Animals).

Related

Wordpress how to create custom post template

Wordpress how to create custom post template from parts of INDEX.php! On my Frontpage (Index.php) i have a great Post Grid. I would like to have this Grid also under each post (Under the Post Content between Comment field). I have copy some parts from the (Index.php) Code and inserted in the post.php but, the result was not satisfactorily.
I have searched everywhere but can not find a satisfying solution, which is compatible with my wordpress theme. If someone can help me, please let me know what code do you need. I would be more than happy to hear a solution!
Thanks in Advance
Not very clear what you mean, but I think you want to print some posts on your single.php file. To accomplished this, you'll need a WP_Query similar to:
<?php
// the query
$args = array(
'post_type' => 'post' // change as needed
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2> <!-- Copy same structure and data as the one on the index.php file -->
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
You will need to keep the same div structure and classes as the one on the index.php to make them look alike.
Good luck!

Page with posts of specific category

I need the option to build a page showing all posts of a specific category.
Showing all posts of a category can be done out-of-the-box by wordpress, I know. But I need the possibility to put some information about all those posts.
I know there's a plugin called "List category posts" (http://wordpress.org/plugins/list-category-posts/). It works but it's only showing the links to the posts. I need the full posts (like they are shown on the "blog page").
If you need to "do something" to results, look at
query_posts
via http://codex.wordpress.org/Function_Reference/query_posts
Here is a sketch that I think leans towards your needs using a custom loop. This can be inserted as needed via simple logic in your template:
// this sets up the filter parameters for a category id some_cat_id sorting asc
$args = array(
'cat' => $some_cat_id,
'order' => 'ASC'
);
// The query is applied via a conditional
if($some_conditional) { // for what ever reason we use the filter+args
query_posts( $args );
// this is also an opportunity to "do stuff" before the loop/output
}
// The Loop (simple example)
while ( have_posts() ) :
the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
As a long time WP user I avoid plugins at all costs in preference of writing sustainable code. Plugins are a point of failure and some of the biggest plugin factories out there are nothing but security issues wrapped in sugar.
Custom loops via conditionals using query "filtering" is amazing and this pattern can be extended to category, search, tags, and meta key:value pairs.
Additionally, by understanding the loop the formatting and output can be controlled in a manner that is easy to sustain. Some of the plugin logic is horrid and very inefficient, so always investigate any and all plugins when performance and security are important.
Here's what I find to be the most simple way to do this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//You can change up the format below any way you'd like.
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
</li>
<?php endwhile; endif; ?>
You can add this to a theme template file and all you need to change is the category id to the category you are trying get posts from. For example if your category id is '114' and you would like to show 9 posts it would look like the following:
<?php query_posts('cat=114&showposts=9'); ?>
If you need to add more info to the posts you should consider using custom fields to do that. Check out the plugin called Advanced Custom Fields.
Here is an example of a custom field being used in a loop:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div class="contentbox"><?php the_excerpt(); ?> </div>
<?php $article_link=get_post_meta($post->ID, 'article-link', true);?>
<?php if ( $article_link ) : ?>
<?php else : ?>
<?php endif; ?>
</li>
<?php endwhile; endif; ?>
In the above example, if the custom field 'article-link' has a value, then that value (a URL) is used as the href in a link instead of the permalink of the article.
Hope I have helped!

How do I tell wordpress that a template is an archive

The use case is that I want to list the most recent posts on a page. Each post containing only the summary and title and a link to the full post.
I have created a template called Latest Posts and it loads ok. However I want get_content() to somehow understand that this template is an archive template and therefor only display the excerpt and not the full post.
I use the following "content" template for all other listings and would like to reuse it when listing my latest posts, but since it isn't either a search or an archive the first selection is skipped, and the_content() will show the whole post.
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'bbl' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'bbl' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
Any ideas? I've been looking around for ideas, and I've seen a lot of different ways to make archives, but I haven't been able to put it all together to fit my use case.
The solution is to use this code:
global $more; // Declare global $more (before the loop).
....
$more = 0;
....
More on this subject can be found in the documentation:
http://codex.wordpress.org/Function_Reference/the_content#Overriding_Archive.2FSingle_Page_Behavior
Now the_content() will not show anything after the more-tag
It's not really telling the engine that this is an archive page, but only mimic the behaviour

Wordpress: different single post template if subscriber is logged in

How can I "tell" Wordpress to choose a different single post template if the user making the request happens to be a logged in subscriber?
This is what I want to do.
Our company distributes products.
The general public can check out the individual products (single posts) and have access to some information (public pricing, etc.).
The plan is to manually add our resellers as subscribers in WP admin, let them log in as subscribers in our website, and then they would be able to see a similar single post, but with some more information thrown in (reseller pricing, volume discounts, links to service manuals, stuff like that).
Is this easy to accomplish?
Thanks!
Are you comfortable with PHP? What you'd have to do is find the template directory and edit (or create) single.php and use get_template_part to display the correct single.php template:
<?php get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_user_logged_in() ) :
<?php get_template_part( 'single' ); ?>
<?php else: ?>
<?php get_template_part( 'single', 'guest' ); ?>
<?php endif; ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
P.S. I copied that single.php page from https://github.com/Automattic/_s/blob/master/single.php
I would handle this with the built-in WordPress function is_user_logged_in().
From a high level, I would actually create a code snippet in my single.php template like so:
if (is_user_logged_in()) {
include 'single_subscriber.php';
}
else {
include 'single_visitor.php';
}
Then you can actually keep single.php clean and simple. But put your actual templates for different users in different files.
Of course, if you have common functionality or logic it would be best to do all of those common things in single.php where possible, and perhaps tweak my example to only add more items using that built-in function.

how to read data stored in a wordpress plugin

I have a wordpress plugin (broadstreet) which is a company directory and has list of companies. But I want to display in different style to match the rest of the website. Now all the company details are stored in the plugin. Is it possible for me to read the data stored in the plugin and display in a page and publish the same using wordpress? If so how?
Any help is greatly appreciated. Thanks
Yes you can, In this case, what I do is create a custom template page. For that you can go to theme you are refering and make a copy of page.php file and rename it. let's say it is directory-page.php.
Next go to admini panel and create a new menu item and set it is template to derectory-page.
Now you can open it and customise as you expect it.
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php //get_template_part( 'content', 'page' ); ?>
<?php// comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
If you want to remove the blog content and call your directry displaying method.
simply, comment out
<?php //get_template_part( 'content', 'page' ); ?>
<?php// comments_template( '', true ); ?>
<?php echo call_directory_display();?>
Now you have to implement call_directory_display() method in your plugin to show content in derectory-page.
If you have any questions ask in comments.

Resources